home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 2 / L'Effet Pommier - Volume 02.iso / Arcade / Marathon Bonus / WAD exporters / dmsnd.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  3KB  |  158 lines

  1. #include <Sound.h>
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stddef.h>
  6.  
  7. #define SHORT(x) ((long)(x)[0]|(long)(x)[1]<<8)
  8.  
  9. typedef struct {
  10.     unsigned char three[2];
  11.     unsigned char rate[2];
  12.     unsigned char len[2];
  13.     unsigned char zero[2];
  14. } sndhed;
  15.  
  16. #if GENERATINGPOWERPC
  17. #pragma options align=mac68k
  18. #endif
  19. typedef struct {
  20.     short                        format;
  21.     short                        numModifiers;
  22.     ModRef                        modifierPart;
  23.     short                        numCommands;
  24.     SndCommand                    commandPart;
  25.     SoundHeader                    head;
  26. } sss;
  27. #if GENERATINGPOWERPC
  28. #pragma options align=reset
  29. #endif
  30.  
  31. static char wadname[]="doom.wad";
  32. static char inname[]="doom sounds.exp";
  33. static unsigned char outname[]="\pdoom sounds.rsrc";
  34.  
  35. void main(void)
  36. {
  37.     char sname[8];
  38.     long soff,slen;
  39.     sndhed sh;
  40.     FILE *wad,*in;
  41.     FInfo fi;
  42.     short resref;
  43.     OSErr err;
  44.     long samples;
  45.     Fixed rate;
  46.     short sndid;
  47.     sss *sndptr;
  48.     Handle sndhand;
  49.     long len;
  50.  
  51.     wad=fopen(wadname,"rb");
  52.     if (!wad) {
  53.         perror(wadname);
  54.         return;
  55.     }
  56.     in=fopen(inname,"r");
  57.     if (!in) {
  58.         perror(inname);
  59.         return;
  60.     }
  61.     CreateResFile(outname);
  62.     err=ResError();
  63.     if (err!=noErr) {
  64.         fprintf(stderr,"CreateResFile: %d\n",err);
  65.         return;
  66.     }
  67.     err=GetFInfo(outname,0,&fi);
  68.     if (err!=noErr) {
  69.         fprintf(stderr,"GetFInfo: %d\n",err);
  70.         return;
  71.     }
  72.     fi.fdType='rsrc';
  73.     fi.fdCreator='RSED';
  74.     err=SetFInfo(outname,0,&fi);
  75.     if (err!=noErr) {
  76.         fprintf(stderr,"SetFInfo: %d\n",err);
  77.         return;
  78.     }
  79.     resref=OpenResFile(outname);
  80.     if (resref==-1) {
  81.         err=ResError();
  82.         fprintf(stderr,"OpenResFile: %d\n",err);
  83.         return;
  84.     }
  85.     for (sndid=8192; ; sndid++) {
  86.         if (fscanf(in," DS%s %ld %ld",sname+1,&soff,&slen)!=3)
  87.             break;
  88.         printf("%s %ld %ld\n",sname+1,soff,slen);
  89.         if (slen<=sizeof (sndhed)) {
  90.             fprintf(stderr,"%s: Resource too short\n",sname+1);
  91.             continue;
  92.         }
  93.         if (fseek(wad,soff,SEEK_SET)) {
  94.             perror(sname+1);
  95.             continue;
  96.         }
  97.         if (fread(&sh,sizeof sh,1,wad)!=1) {
  98.             fprintf(stderr,"%s: Unexpected EOF\n",sname+1);
  99.             continue;
  100.         }
  101.         if (SHORT(sh.three)!=3 || SHORT(sh.zero)!=0) {
  102.             fprintf(stderr,"%s: Not a sound resource\n",sname+1);
  103.             continue;
  104.         }
  105.         samples=SHORT(sh.len);
  106.         rate=SHORT(sh.rate)<<16;
  107.         if (samples+sizeof sh>slen) {
  108.             fprintf(stderr,"%s: Invalid sound header\n",sname+1);
  109.             continue;
  110.         }
  111.         sndhand=NewHandle(offsetof(sss,head.sampleArea)+samples);
  112.         if (!sndhand) {
  113.             err=MemError();
  114.             fprintf(stderr,"%s: NewHandle: %d\n",sname+1,err);
  115.             continue;
  116.         }
  117.         HLockHi(sndhand);
  118.         sndptr=*(sss **)sndhand;
  119.         sndptr->format=1;
  120.         sndptr->numModifiers=1;
  121.         sndptr->modifierPart.modNumber=sampledSynth;
  122.         sndptr->modifierPart.modInit=initMono;
  123.         sndptr->numCommands=1;
  124.         sndptr->commandPart.cmd=bufferCmd|dataOffsetFlag;
  125.         sndptr->commandPart.param1=0;
  126.         sndptr->commandPart.param2=offsetof(sss,head);
  127.         sndptr->head.samplePtr=NULL;
  128.         sndptr->head.length=samples;
  129.         sndptr->head.sampleRate=rate;
  130.         sndptr->head.loopStart=0;
  131.         sndptr->head.loopEnd=0;
  132.         sndptr->head.encode=stdSH;
  133.         sndptr->head.baseFrequency=kMiddleC;
  134.         len=fread(sndptr->head.sampleArea,1,samples,wad);
  135.         if (len<samples) {
  136.             fprintf(stderr,"%s: Unexpected EOF\n",sname+1);
  137.             DisposeHandle(sndhand);
  138.             continue;
  139.         }
  140.         HUnlock(sndhand);
  141.         sname[0]=strlen(sname+1);
  142.         AddResource(sndhand,'snd ',sndid,(StringPtr)sname);
  143.         err=ResError();
  144.         if (err!=noErr) {
  145.             fprintf(stderr,"%s: AddResource: %d\n",sname+1,err);
  146.             DisposeHandle(sndhand);
  147.             continue;
  148.         }
  149.         WriteResource(sndhand);
  150.         ReleaseResource(sndhand);
  151.     }
  152.     CloseResFile(resref);
  153.     fclose(wad);
  154.     fclose(in);
  155.     return;
  156. }
  157.  
  158.